package com.easyShop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyShop.common.QueryPageParam;
import com.easyShop.common.Result;
import com.easyShop.entity.OrderReturnApply;
import com.easyShop.service.OrderReturnApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 订单退货申请 前端控制器
 * </p>
 *
 * @author wms
 * @since 2024-12-03
 */
@RestController
@RequestMapping("/orderReturnApply")
public class OrderReturnApplyController {
    @Autowired
    private OrderReturnApplyService orderReturnApplyService;

    @GetMapping("/list")
    public Result list() {
        List list = orderReturnApplyService.list();
        return Result.suc(list);
    }
    @PostMapping("/save")
    public Result save(@RequestBody OrderReturnApply orderReturnApply) {
        return orderReturnApplyService.save(orderReturnApply)?Result.suc():Result.fail();
    }
    @PostMapping("/update/{id}")
    public Result update(@PathVariable String id, @RequestBody OrderReturnApply orderReturnApply) {
        Integer IntId = Integer.parseInt(id);
        LambdaQueryWrapper<OrderReturnApply> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(OrderReturnApply::getOrderId, IntId);

        return orderReturnApplyService.updateById(orderReturnApply)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String idStr = (String) param.get("id"); // 获取 id 作为 String
        Integer id = StringUtils.isNotBlank(idStr) ? Integer.parseInt(idStr) : null; // 安全地转换为 Integer

        Page<OrderReturnApply> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<OrderReturnApply> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(id != null ){
            lambdaQueryWrapper.like(OrderReturnApply::getId,id.toString());
        }else if(id == null){
            // 当id为null时，不添加任何条件，以获取所有数据
            // 这里不需要添加任何代码，因为默认就是查询所有数据
        }

        IPage result = orderReturnApplyService.pageCC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return orderReturnApplyService.removeById(id)?Result.suc():Result.fail();
    }
}
